package com.java;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步回调
 * @author jingjintao
 * @date 2021/10/28 - 22:15
 **/
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t update mysql ok");
        });
        voidCompletableFuture.get();

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t insert mysql ok");
//            int age = 10 / 0;
            return 1024;
        });

        System.out.println(integerCompletableFuture.whenComplete((t, u) -> {
            System.out.println("***** t:" + t);
            System.out.println("***** u:" + u);
        }).exceptionally(e -> {
            System.out.println(e.getMessage());
            return 4444;
        }).get());
    }
}
